package org.joinme.backend.controller;

import org.joinme.backend.model.ApplicationRecord;
import org.joinme.backend.model.CarPoolingRecord;
import org.joinme.backend.model.ParticipateRecord;
import org.joinme.backend.service.ApplicationService;
import org.joinme.backend.service.CarPoolingService;
import org.joinme.backend.service.ParticipateService;
import org.joinme.backend.service.WechatUserService;
import org.joinme.backend.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = {"/{appName}/wechat/application"}, produces = {"application/json;charset=UTF-8"})
public class ApplicationController {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WechatUserService userService;

    @Autowired
    private ApplicationService applicationService;

    //添加拼单记录
    @ResponseBody
    @RequestMapping("/record/add")
    public String addCarPoolingRecord(HttpServletRequest request, ApplicationRecord record){
        try {
            //获取登陆的用户信息
            int userId = userService.getLoginUserId(request);
            record.setUserId(userId);
            //添加申请记录
            int applicationId = applicationService.addApplicationRecord(record);
            if(applicationId > 0) {
                return ApiResult.SUCCESS;
            } else
                throw new PrjException(ErrorCodeMsg.APPLICATION_RECORD_ADD_ERROR);
        } catch (PrjException e){
            log.error(ApiJson.toJson(request.getParameterMap()));
            log.error("Error message : " + e.getErrMsg());
            return ApiResult.writeException(e);
        } catch (Exception e) {
            log.error(ApiJson.toJson(request.getParameterMap()));
            log.error(PrjException.getTrace(e));
            return ApiResult.SYSTEM_ERROR;
        }
    }

    //查询申请列表
    @ResponseBody
    @RequestMapping("/record/list")
    public String listDreamRecord(HttpServletRequest request,
                                  @RequestParam(required = false, defaultValue = "0") int start,
                                  @RequestParam(required = false, defaultValue = "10") int limit){
        try {
            int userId = userService.getLoginUserId(request);

            if(start == 0)
                return ApiResult.writeData(applicationService.getApplicationList(userId, start, limit), applicationService.countApplicationList(userId));
            else
                return ApiResult.writeData(applicationService.getApplicationList(userId, start, limit));
        } catch (PrjException e){
            return ApiResult.writeException(e);
        } catch (Exception e) {
            log.error(ApiJson.toJson(request.getParameterMap()));
            log.error(PrjException.getTrace(e));
            return ApiResult.SYSTEM_ERROR;
        }
    }

    //更新申请状态
    @ResponseBody
    @RequestMapping("/record/update")
    public String updateCarPoolingRecord(HttpServletRequest request, ApplicationRecord record){
        try {
            //获取登陆的用户信息
            int userId = userService.getLoginUserId(request);

            //userId等于-1，表示当前登录用户放弃申请
            if(record.getUserId() == -1){
                record.setUserId(userId);
                record.setApplicationStatus(ConstUtil.APPLICATION_ABORTED);
            }

            //更新状态
            if(applicationService.updateApplicationRecord(record)) {
                return ApiResult.SUCCESS;
            } else
                throw new PrjException(ErrorCodeMsg.APPLICATION_RECORD_UPDATE_ERROR);
        } catch (PrjException e){
            log.error(ApiJson.toJson(request.getParameterMap()));
            log.error("Error message : " + e.getErrMsg());
            return ApiResult.writeException(e);
        } catch (Exception e) {
            log.error(ApiJson.toJson(request.getParameterMap()));
            log.error(PrjException.getTrace(e));
            return ApiResult.SYSTEM_ERROR;
        }
    }
}
